define(['jquery','fullcalendar','fullcalendar_zh-cn','formServ',
	'init','bsMsgServ','userSelect','moment','bootstrap-datetimepicker','bootstrap-datetimepicker.zh-CN'],
		function($,fullcalendar,lang,formServ,
			init,bsMsgServ,userSelect,moment,datetimepicker,datetimepickerLang){
		
		//var qryUrl='/'+sysmanMain.getRootPath()+'/oaDutyList/query/getDutyList.action';
		
		var rootid = 'calendar-main';
		
		var btnEvent = {
			add:function(e){
				$('#'+rootid +' .main-form').form('clear');
				funcDef.initUser();
				$(e.currentTarget).removeClass('disabled');
			}
			,
			save:function(e){
				var _f = $('#'+rootid +' form.main-form')
				//判断form必填项
				if(
					!formServ.validateForm({
						form:_f,
						rules:funcDef.getFormRule()
					})
				){
					bsMsgServ.errorSlide('请填写表单内容');
					//释放按钮禁用状态
					$(e.currentTarget).removeClass('disabled');
					return
				}
				var _data = formServ.getDataObj({form:_f});

				var _param = {
					url :'/'+init.getSite()+'/oaDutyList/save.action',
					dataType : 'json',
					data : $.toJSON(_data),
					type : 'post',
					contentType : "appliction/json;charset=utf-8",
					success:function(data){
						bsMsgServ.msgSlide('操作成功！');
						$('#'+rootid +' form.main-form').form('load',data);
						$('#'+rootid +' #calendar-root').fullCalendar( 'refetchEvents');
						$(e.currentTarget).removeClass('disabled');
					},
					error:function(){
						bsMsgServ.errorSlide('操作失败！')
						$(e.currentTarget).removeClass('disabled');
					}
				}
				$.ajax(_param)			
				
			},
			del:function(e){
				var _id = $('#'+rootid +' #id')
				if(_id.length==1 && _id.val()){
					$.messager.confirm('警告', '确定执行删除操作?', function(r){
						
						if( r){	
							var idv = _id.val();
							var _param = {
								url :'/'+init.getSite()+'/oaDutyList/del.action',					
								data : {id:idv},
								type : 'post',					
								success:function(){
									$('#'+rootid +' #calendar-root').fullCalendar( 'removeEvents',idv);
									$('#'+rootid +' form.main-form').form('clear');
									bsMsgServ.msgSlide('操作成功！')
									$(e.currentTarget).removeClass('disabled');
								},
								error:function(){
									bsMsgServ.errorSlide('操作失败！')
									$(e.currentTarget).removeClass('disabled');
								}			
							
							}
							$.ajax(_param)
						}
					})
				}else{
					bsMsgServ.msgSlide('请选择要删除的日程！')
					$(e.currentTarget).removeClass('disabled');
				}
			}
		}
		
		var funcDef = {
			
			refreshForm:function(data){
				var _form = $('#'+rootid +' form.main-form')
				if(_form.length>0){
					_form.form('clear');
					_form.form('load',data)
				}
			},
			btnClick:function(e){
				
				init.applyBtnEvt({btnEvt:e,btnConfig:btnEvent});
			}
			
			,
			initUser:function(){
				//初始化用户
				var _usr =init.getCurrentUser();
				var _el = $('#'+rootid+ ' #apply_name')
				if(_el){
					_el.val(_usr.name)
					
				}
				
				_el = $('#'+rootid+ ' #apply_id')
				if(_el){
					_el.val(_usr.id)
				}
				
				//初始化部门				
				_el = $('#'+rootid+ ' #apply_deptname')
				if(_el){
					_el.val(_usr.deptName)
					
				}
				
				_el = $('#'+rootid+ ' #apply_deptid')
				if(_el){
					_el.val(_usr.deptid)
				}	
			},
			initDatepicker:function(){
				
				var _start = $('#'+rootid + ' #start_time')
				var _end = $('#'+rootid + ' #end_time')
				
				var _param ={
					language:  'zh-CN',
					format: "yyyy-mm-dd hh:ii:ss",
					autoclose: true,
					minView: "month",
					maxView: "decade",
					todayBtn: true,
					pickerPosition: "bottom-left"
				}
				
				if(_start.length==1){
					if(window.console){
						console.log( ' start length: ' + 1)
					}
					_start.datetimepicker(_param);
					
					if(_end.length==1){
						if(window.console){
							console.log( ' start length: ' + 1)
						}
						_start.on("click",function(ev){
							_start.datetimepicker("setEndDate", _end.val());
						})
						
						_end.datetimepicker(_param)
							.on("click", function (ev) {
								_end.datetimepicker("setStartDate", _start.val());
							})
					}
				}				
			}
			,
			//初始化calendar
			initCalendar:function(_options){
					//var queryParam;
				var qryUrl='/'+init.getSite()+'/oaDutyList/query/getDutyList.action';
				
				$('#'+rootid +' #calendar-root').fullCalendar({
					header: {
						left: 'prev,next today',
						center: 'title',
						right: 'month,agendaWeek,agendaDay'
					},
					editable: false,
					eventLimit: true, // allow "more" link when too many events
					lang:'zh-cn',				    
				    eventLimit: true,
				    theme:false,
				    events:qryUrl,
			        eventClick: function(events, jsEvent, view) {
			        	if(events && events._id){
				        	var _param = {
								url :'/'+init.getSite()+'/oaDutyList/query/getOne.action?id='+events._id,					
								type : 'post',
								contentType : "appliction/json;charset=utf-8",
								success:funcDef.refreshForm,
								error:function(){
									bsMsgServ.errorSlide('操作失败！')
								}
							}
							$.ajax(_param)
						}			        	
					}				    
				});			
			}
			,
			
			getFormRule:function(){
				var rules = {
					rules:{
						apply_deptname: 'required',
						apply_name: 'required',
						start_time: 'required',
						end_time: 'required',
						item: 'required'
						
					}
					,
					messages:{
						apply_deptname: '部门不允许为空',
						apply_name: '人员不允许为空',
						start_time: '开始时间不允许为空',
						end_time: '结束时间不允许为空',
						item: '内容不允许为空'
					}
				}
				
				return rules;
			}
		}

		var public_method = {
			initPage:function(){
					
				//初始化日程组件
				funcDef.initCalendar();
				/*****绑定按钮函数*******/
			    var $btn= $("#" +rootid +" .btn")
			    
			    if(window.console){
					console.log('length:'+$btn.length)
				}
				$btn.unbind('click');
				$btn.bind('click',funcDef.btnClick)	
				
				/******弹窗选择人员及部门********/
				var el = $('#'+rootid+ ' #apply_name')
				console.log('el.length:' + el.length)
//				$(el).textbox({
//					onClickButton:function(data){								
//						deptUserSelect.openPage({
//							userids:rootid +' #apply_id',
//							usernames:rootid +' #apply_name',
//							deptid:rootid +' #apply_deptid',
//							deptname:rootid +' #apply_deptname'					
//						})								
//					}
//				})
				
				//初始化用户
				funcDef.initUser();
				funcDef.initDatepicker();
//				$.messager.progress('close');	
			}
		}
		return public_method;
	});